<?php

/**
 * 发布网店商品，每分钟检测一次
 *
 **/

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\SuperSubsidy\YouZanBrandGoodsModel;
use Biz\service\SuperSubsidy\YouZanGoods;

$id = $argv[1] ?? 0;

$where = [
    'status' => YouZanBrandGoodsModel::STATUS_WAIT_RELEASE
];
if ($id) {
    $where['id'] = $id;
}
var_dump('检测待发布商品');
$list = YouZanBrandGoodsModel::selectByWhere($where);
foreach ($list as $row) {
    try {
        $spuCode = $row['spu_code'];
        $data = $row;
        $data['no_sku_info'] = json_decode($row['no_sku_info'], true);
        $data['sku_info'] = json_decode($row['sku_info'], true);
        $res = YouZanGoods::brandSpuRelease($spuCode, $data);
        if (!$res) {
            var_dump('发布商品失败', ['id' => $row['id'], 'spu_code' => $spuCode]);
        }
        var_dump('发布商品成功#spu_code:' . $spuCode);
    } catch (Exception $e) {
        var_dump('发布商品失败', ['id' => $row['id'], 'spu_code' => $spuCode, 'code' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
    }
}
var_dump('检测完毕');